﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO.IsolatedStorage;
using System.IO;
using System.Runtime.Serialization;


namespace EMOe
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        private List<Emoticon> favlist=new List<Emoticon>();
        public MainPage()
        {
            InitializeComponent();
            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }

        private void saveStorage(int option, Emoticon selectobj) {
            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
            xmlWriterSettings.Indent = true;
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Fav.xml", FileMode.Create))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<Emoticon>));
                    using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
                    {
                        if(option==1)
                            serializer.Serialize(xmlWriter, AddFav(selectobj));
                        else if(option==2)
                            serializer.Serialize(xmlWriter, DeleteFav(selectobj));
                    }
                }
            }
        }

		private void btnFav_Click(object sender, RoutedEventArgs e)
		{
    		var item = ((Button) sender).DataContext;
            Emoticon selectobj = (Emoticon)item;
            saveStorage(1, selectobj);
            changefav();
		}

        private List<Emoticon> AddFav(Emoticon e)
        {
            if(!favlist.Contains(e))favlist.Add(e);
            return favlist;
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            var item = ((Button)sender).DataContext;
            Emoticon selectobj = (Emoticon)item;
            saveStorage(2, selectobj);
        }

        private List<Emoticon> DeleteFav(Emoticon e)
        {
            if (favlist.Contains(e)) favlist.Remove(e);
            return favlist;
        }

        private void lstEmo_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
        	
            Emoticon selectobj = (Emoticon)lstEmo.SelectedItem;
            Clipboard.SetText(selectobj.emo);
            MessageBox.Show("copy: "+selectobj.emo);

        }

        private void ApplicationBarIconButton_Click(object sender, System.EventArgs e)
        {
        	// TODO: Add event handler implementation here.
        }

        private void Pivot_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
        	// load emo
            XDocument loadedData = XDocument.Load("emocollection.xml");
            var data = from query in loadedData.Descendants("emoticon")
                       select new Emoticon
                       {
                           emo = (string)query.Element("emo"),
                           category = (string)query.Element("category"),
                           
                       };
            lstEmo.ItemsSource = data;
            changefav();
            

        }

        private void changefav(){
            try
            {
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Fav.xml", FileMode.Open))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(List<Emoticon>));
                        List<Emoticon> favdata = (List<Emoticon>)serializer.Deserialize(stream);
                        lstFav.ItemsSource = favdata;
                    }
                }
                
            }
            catch
            {
                
            }
        }

    }
}